const database = require('mime-db');

class VrDB {
    constructor(){}

    /**
     * 插入新数据
     * @param {*} sql 
     * @param {*} values 
     * @param {*} callback  ({result, message})=>{}
     */
    insert(sql, values, callback=undefined) {}

    /**
     * 更新数据
     * @param {*} sql 
     * @param {*} values 
     * @param {*} callback 
     */
    update(sql, values, callback=undefined) {}

    /**
     * 删除数据
     * @param {*} sql 
     * @param {*} values 
     * @param {*} callback ({result, message})=>{}
     */
    delete(sql, values=undefined, callback=undefined) {}

    /**
     *  获取数据
     * @param {*} sql 
     * @param {*} values 
     * @param {*} callback ({result, message, datas})=>{}
     */
    select(sql, values=undefined, callback=undefined) {}

    /**
     *  释放数据库链接资源
     */
    release() {}
}


//---------------------------- sqlite3 -------------------------------------------------------------------------------///
const sqlite3 = require('sqlite3').verbose();
class VrSqliteDB extends VrDB {
    constructor() {
        super()
        this.db = new sqlite3.Database('./resource/database.db',
                                    sqlite3.OPEN_READWRITE,
                                    (err)=>{
                                        if(err) {
                                            console.error('connect sqlite database failed. ' + err.message)
                                        } else {
                                            console.log('connect sqlite database successfully')
                                        }
                                    })
    }

    release() {
        this.db.close((err)=>{
            if(err) {
                return console.error('close sqlite database failed. ' + err.message)
            } else {
                console.log('close sqlite database connection')
            }
        })
    }

    insert(sql, values, callback) {
        this.db.run(sql, values, (err)=>{
            let ret = { result: false, message: ''}
            if(err) {
                ret.message = err.message
                console.error('sqlite3 insert data error: ' + err.message)
            } else {
                ret.result = true
            }

            if(callback !== undefined && callback !== null) {
                callback(ret)
            }
        })
    }

    update(sql, values, callback) {
        this.db.run(sql, values, (err)=>{
            let ret = { result: false, message: ''}
            if(err) {
                ret.message = err.message
                console.error('sqlite3 update data error: ' + err.message)
            } else {
                ret.result = true
            }

            if(callback !== undefined && callback !== null) {
                callback(ret)
            }
        })
    }

    delete(sql, values, callback) {
        this.db.run(sql, values, (err)=>{
            let ret = { result: false, message: ''}
            if(err) {
                ret.message = err.message
                console.error('sqlite3 delete data error: ' + err.message)
            } else {
                ret.result = true
            }

            if(callback !== undefined && callback !== null) {
                callback(ret)
            }
        })
    }

    select(sql, values=undefined, callback=undefined) {
        this.db.all(sql, values, (err, rows)=>{
            let ret = { result: false, message: ''}
            if(err) {
                ret.message = err.message
                console.error('sqlite3 select data error: ' + err.message)
            } else {
                console.log('sqlite3 select data successfully. ' + JSON.stringify(rows))
                ret.result = true
                ret.data = rows
            }

            if(callback !== undefined && callback !== null) {
                callback(ret)
            }
        })
    }
}

function create(dbtype = 0, values = null) {
    let database = null
    switch(dbtype) {
        case 0:
            database = new VrSqliteDB()
    }

    return database
}

module.exports = {
    create
}